//
//  SAMemberBalanceCell.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/6/28.
//

#import "SAMemberBalanceCell.h"
#import "SAMemberBalanceModel.h"
#import "UILabel+NSMutableAttributedString.h"

@interface SAMemberBalanceCell ()

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *dateLab;
@property (nonatomic, strong) UILabel *priceLab;
@property (nonatomic, strong) UILabel *storeLab;

@end

@implementation SAMemberBalanceCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setModel:(SAMemberBalanceModel *)model {
    _model = model;
    NSArray *arr = [model.remark componentsSeparatedByString:@"，"];
    if (arr.count > 0) {
        self.titleLab.text = arr[0];
    }
    self.dateLab.text = model.createTime;
    self.priceLab.text = model.points;
    if ([model.points doubleValue] < 0) {
        _priceLab.textColor = kBuleColor;
        _priceLab.text = model.points;
    }else {
        _priceLab.textColor = kPinkColor;
        _priceLab.text = [NSString stringWithFormat:@"+%@",model.points];
    }
    self.storeLab.text = model.storeName;
}

-(void)setBalanceModel:(SAMemberBalanceModel *)balanceModel {
    _balanceModel = balanceModel;
    self.priceLab.numberOfLines = 1;
    NSArray *arr = [balanceModel.remark componentsSeparatedByString:@"，"];
    if (arr.count > 0) {
        self.titleLab.text = arr[0];
    }
    self.dateLab.text = balanceModel.createTime;
    double price = 0.0;
    if ([self.titleLab.text isEqualToString:@"退款金额"]) {
        price = [balanceModel.amount doubleValue];
        if (price < 0) {
            self.priceLab.text = [NSString stringWithFormat:@"%0.2lf",price/100];
            self.priceLab.textColor = kBuleColor;
        }else {
            self.priceLab.text = [NSString stringWithFormat:@"+%0.2lf",price/100];
            self.priceLab.textColor = kPinkColor;
        }
    }else if ([self.titleLab.text isEqualToString:@"消费金额"]) {
        price = [balanceModel.addupConsumeAmount doubleValue];
        if (price < 0) {
            self.priceLab.text = [NSString stringWithFormat:@"%0.2lf",price/100];
            self.priceLab.textColor = kBuleColor;
        }else {
            self.priceLab.text = [NSString stringWithFormat:@"+%0.2lf",price/100];
            self.priceLab.textColor = kPinkColor;
        }
    }else if ([self.titleLab.text isEqualToString:@"充值金额"]) {
        self.priceLab.numberOfLines = 2;
        price = [balanceModel.amount doubleValue];
        NSString *str = [NSString stringWithFormat:@"(赠送%0.2lf)",[balanceModel.givenAmount doubleValue]/100];
        self.priceLab.text = [NSString stringWithFormat:@"+%0.2lf\n%@",price/100,str];
        [self.priceLab setAttributedStringWithSubString:str font:kFont(12)];
        self.priceLab.textColor = kPinkColor;
    }
    self.storeLab.text = balanceModel.storeName;
}

-(void)initUI {
    self.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.dateLab];
    [self.contentView addSubview:self.priceLab];
    [self.contentView addSubview:self.storeLab];
}

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:SARectMake(26, 0, 70, 60)];
        _titleLab.textColor = kBlackColor;
        _titleLab.font = kFont(14);
    }
    return _titleLab;
}

-(UILabel *)dateLab {
    if (!_dateLab) {
        _dateLab = [[UILabel alloc] initWithFrame:SARectMake(kBaseScreenWidth - 160, 30, 145, 20)];
        _dateLab.textColor = kBlackColor;
        _dateLab.font = kFont(14);
        _dateLab.textAlignment = NSTextAlignmentRight;
    }
    return _dateLab;
}

-(UILabel *)priceLab {
    if (!_priceLab) {
        _priceLab = [[UILabel alloc] initWithFrame:SARectMake(100, 0, 115, 60)];
        _priceLab.textColor = kBuleColor;
        _priceLab.font = kFont(14);
        _priceLab.textAlignment = NSTextAlignmentCenter;
    }
    return _priceLab;
}

-(UILabel *)storeLab {
    if (!_storeLab) {
        _storeLab = [[UILabel alloc] initWithFrame:SARectMake(kBaseScreenWidth - 160, 10, 145, 20)];
        _storeLab.textColor = kBlackColor;
        _storeLab.font = kFont(14);
        _storeLab.textAlignment = NSTextAlignmentRight;
    }
    return _storeLab;
}

@end
